/**
 *	The NeuroCoSA Toolkit
 *	Copyright (C) 2003-6 Stuart Meikle.
 *
 *	This is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation; either
 *	version 2.1 of the License, or (at your option) any later version.
 *
 *	This library is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	Lesser General Public License for more details.
 *
 * @author	Stuart Meikle
 * @version	2006-halloween
 * @license	LGPL
 */
package org.stumeikle.NeuroCoSA.AutoCluster;

/* Comments:
 * 20060125	stumeikle	new classes to abstract away the data type.
 *				used by the STLTMemory
*/

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.event.*;

public	class		N2DVector implements NMeasurable,Cloneable
{
    double	x,y;

    // Interface implentation --------------------------------------------
    public	double		distanceTo( NMeasurable p )
    {
	return distanceTo( (N2DVector) p );
    }
    
    public	double	        distanceSquaredTo( NMeasurable p )
    {
    	return distanceSquaredTo( (N2DVector) p );
    }
    
    public	void		add( NMeasurable p )
    {
    	add((N2DVector)p);
    	return;
    }
        
    public	void		subtract( NMeasurable p )
    {
    	subtract((N2DVector)p);
    	return;
    }

    public	double		dotProduct( NMeasurable p )
    {
        return dotProduct( (N2DVector) p );
    }
    
    public	void		divideByScalar( double d )
    {
	if (d>0.0001 || d< -0.001)
	{
	    x/=d;
	    y/=d;
	}
    }
    
    public	void		multiplyByScalar( double d )
    {
        x*=d;
        y*=d;
    }
    
    public	NMeasurable	createNewZero()
    {
    	//create a new data type , set to zero and return 
    	N2DVector		s = new N2DVector();
    	
    	s.x =0.0;
   	s.y =0.0;
	return	(NMeasurable)s;
    }


    // internal methods -------------------------------------------------
    public	N2DVector()
    {
	x = y = 0.0;
    }
    public	N2DVector( double a, double b )
    {
 	x = a;
	y = b;
    }

    public	double		distanceTo( N2DVector p )
    {
        double	dx,dy;
        
        dx = p.x - x;
        dy = p.y - y;
        
        return Math.sqrt( dx*dx + dy*dy );
    }
    
    public	double		distanceSquaredTo( N2DVector p )
    {
        double	dx,dy;
        
        dx = p.x - x;
        dy = p.y - y;
        
        return dx*dx + dy*dy;
    }
    
    public	double		dotProduct( N2DVector v )
    {
        return x*v.x + y*v.y;
    }
    
    public	void		add( N2DVector v )
    {
    	x += v.x;
    	y += v.y;
    }    
    
    public	void		subtract( N2DVector v )
    {
    	x -= v.x;
    	y -= v.y;
    }    

    public	void		multiply( N2DVector v )
    {
	x *= v.x;
	y *= v.y;
    }

    public	double		magnitude()
    {
        return Math.sqrt( x*x + y*y );
    }
    
    public	NMeasurable	createCopy()
    {
    	try {  return (NMeasurable) this.clone(); }
    	catch( Exception e )
    	{ return null; }
    }
    
    public	Object		clone()
    {
    	try{  return super.clone();}
    	catch(Exception e)
    	{return null;}
    }
    
    public	void		setX( double a )
    { x=a; }
    
    public	void		setY( double a )
    { y=a; }
    public	double		getX()
    { return x; }
    public	double		getY()
    { return y; }
}
